|
1
|
|
|
function arrayCompare(a1: Array<any>, a2: Array<any>) { |
|
2
|
|
|
if (a1.length != a2.length) return false; |
|
3
|
|
|
var length = a2.length; |
|
4
|
|
|
for (var i = 0; i < length; i++) { |
|
5
|
|
|
if (a1[i] !== a2[i]) return false; |
|
6
|
|
|
} |
|
7
|
|
|
return true; |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* in_array PHP equivalent |
|
12
|
|
|
* @param needle string etc |
|
13
|
|
|
* @param haystack |
|
14
|
|
|
*/ |
|
15
|
|
|
function inArray(needle: any, haystack: Array<any>) { |
|
16
|
|
|
var length = haystack.length; |
|
17
|
|
|
for (var i = 0; i < length; i++) { |
|
18
|
|
|
if (typeof haystack[i] == "object") { |
|
19
|
|
|
if (arrayCompare(haystack[i], needle)) return true; |
|
20
|
|
|
} else { |
|
21
|
|
|
if (haystack[i] == needle) return true; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* in_array PHP equivalent |
|
29
|
|
|
* @param needle string etc |
|
30
|
|
|
* @param haystack |
|
31
|
|
|
*/ |
|
32
|
|
|
function in_array(needle: any, haystack: Array<any>) { |
|
33
|
|
|
return inArray(needle, haystack); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* get all keys |
|
38
|
|
|
* @param haystack string etc |
|
39
|
|
|
*/ |
|
40
|
|
|
function array_keys(haystack: any) { |
|
41
|
|
|
return Object.keys(haystack); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Shuffles array in place. |
|
46
|
|
|
* @param a items An array containing the items. |
|
47
|
|
|
*/ |
|
48
|
|
|
function array_shuffle(a: Array<any>) { |
|
49
|
|
|
var j: number, x: any, i: number; |
|
50
|
|
|
for (i = a.length - 1; i > 0; i--) { |
|
51
|
|
|
j = Math.floor(Math.random() * (i + 1)); |
|
52
|
|
|
x = a[i]; |
|
53
|
|
|
a[i] = a[j]; |
|
54
|
|
|
a[j] = x; |
|
55
|
|
|
} |
|
56
|
|
|
return a; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
Array.prototype.unique = function () { |
|
60
|
|
|
var a = this.concat(); |
|
61
|
|
|
for (var i = 0; i < a.length; ++i) { |
|
62
|
|
|
for (var j = i + 1; j < a.length; ++j) { |
|
63
|
|
|
if (a[i] === a[j]) a.splice(j--, 1); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return a; |
|
68
|
|
|
}; |
|
69
|
|
|
|
|
70
|
|
|
if (!Array.prototype.every) { |
|
71
|
|
|
Array.prototype.every = function (fun /*, thisp */) { |
|
72
|
|
|
"use strict"; |
|
73
|
|
|
var t: { [x: string]: any; length: number }, |
|
74
|
|
|
len: number, |
|
75
|
|
|
i: string | number, |
|
76
|
|
|
thisp: any; |
|
77
|
|
|
|
|
78
|
|
|
if (this == null) { |
|
79
|
|
|
throw new TypeError(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
t = Object(this); |
|
83
|
|
|
len = t.length >>> 0; |
|
84
|
|
|
if (typeof fun !== "function") { |
|
85
|
|
|
throw new TypeError(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
thisp = arguments[1]; |
|
89
|
|
|
for (i = 0; i < len; i++) { |
|
90
|
|
|
if (i in t && !fun.call(thisp, t[i], i, t)) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return true; |
|
96
|
|
|
}; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
function array_filter(array: []) { |
|
100
|
|
|
return array.filter(function (el) { |
|
101
|
|
|
return el != null; |
|
102
|
|
|
}); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* pick random from array |
|
107
|
|
|
* @param {Array<any>} arrays |
|
108
|
|
|
* @param {boolean} unique Unique the arrays |
|
109
|
|
|
*/ |
|
110
|
|
|
function array_rand(arrays: any[], unique: any) { |
|
111
|
|
|
if (unique) { |
|
112
|
|
|
arrays = array_unique(arrays); |
|
113
|
|
|
} |
|
114
|
|
|
var index = Math.floor(Math.random() * arrays.length); |
|
115
|
|
|
return { |
|
116
|
|
|
index: index, |
|
117
|
|
|
value: arrays[index], |
|
118
|
|
|
}; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Array unique |
|
123
|
|
|
* @param {Array<any>} arrays |
|
124
|
|
|
*/ |
|
125
|
|
|
function array_unique(arrays: any[]) { |
|
126
|
|
|
return arrays.filter(function (item: any, pos: any, self: string | any[]) { |
|
127
|
|
|
return self.indexOf(item) == pos; |
|
128
|
|
|
}); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* |
|
133
|
|
|
* @param {Array<any>} arrayName |
|
134
|
|
|
* @param {String|number} key |
|
135
|
|
|
*/ |
|
136
|
|
|
function array_unset(arrayName: { [x: string]: any }, key: any) { |
|
137
|
|
|
var x: string | number; |
|
138
|
|
|
var tmpArray = new Array(); |
|
139
|
|
|
for (x in arrayName) { |
|
140
|
|
|
if (x != key) { |
|
141
|
|
|
tmpArray[x] = arrayName[x]; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
return tmpArray; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* PHP shuffle array equivalent |
|
149
|
|
|
* @param array |
|
150
|
|
|
* @example |
|
151
|
|
|
* var arr = [2, 11, 37, 42]; |
|
152
|
|
|
* shuffle(arr); |
|
153
|
|
|
* console.log(arr); //return random |
|
154
|
|
|
*/ |
|
155
|
|
|
function shuffle(array: Array<any>) { |
|
156
|
|
|
var currentIndex = array.length, |
|
157
|
|
|
temporaryValue: any, |
|
158
|
|
|
randomIndex: number; |
|
159
|
|
|
|
|
160
|
|
|
// While there remain elements to shuffle... |
|
161
|
|
|
while (0 !== currentIndex) { |
|
162
|
|
|
// Pick a remaining element... |
|
163
|
|
|
randomIndex = Math.floor(Math.random() * currentIndex); |
|
164
|
|
|
currentIndex -= 1; |
|
165
|
|
|
|
|
166
|
|
|
// And swap it with the current element. |
|
167
|
|
|
temporaryValue = array[currentIndex]; |
|
168
|
|
|
array[currentIndex] = array[randomIndex]; |
|
169
|
|
|
array[randomIndex] = temporaryValue; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return array; |
|
173
|
|
|
} |
|
174
|
|
|
|